<?php
namespace c\user;
\lang::load('user');
/**
 * Description of reg
 *
 * @author YeFng
 */
class reg extends \control
{
	var $table = 'user';
	
	function __construct()
	{
		parent::__construct();
		$this->Ucon = include CONFIG_DIR . 'user.php';
	}
    function index_home()
    {
       $this->loadTemplate('user/reg'); 
    }
    function index_s()
    {
        $b = $this->s();
        ajax_json($b ? \lang::g($b) : TRUE);
    } 
	//保存
    protected function s()
    {
		$p = $_POST;
        $UG = \YF_f::initC('\c\user\g');
        $data = array('reg_time'=> date('YmdHis'),
                       'reg_area' => getIP(),
                       'expiry' => '',
                       'state'=> 1,);
		
		// pass
        if (strlen($p['password'])<3 or trim($p['password'])!=$p['password'])
            return 'password wrongful';
		$data['password'] = $UG->password_ed($p['password']);
        
        // 推荐
        if ($p['parent_id']){
            $data['parent_id'] = $UG->nameToID($p['parent_id']);
            if (!$data['parent_id'])
                return 'undefinition presenter';
         }

        // Name
        $regName = trim($p['regName']);
        if ($p['nt']=='phone')
          {
			if ($this->isHas($regName, 'phone'))
					return 'user already exist';
            $data['phone'] = $regName;
			
          }
        elseif ($p['nt']=='email')
         {
			if ($this->isHas($regName, 'email'))
					return 'user already exist';
			$data['email'] = $regName;
         }
        else{
			if ($this->isHas($regName, 'name'))
					return 'user already exist';
			
            if (strlen($regName)<3 || is_numeric($regName{0}) || strpos($regName, '@')!==false || strpos($regName, '.')!==false)
                return  'user name wrongful';
				
            if (in_array($regName, $this->Ucon['dis_regName']))
                return  'user name wrongful';
            unset($a);
            $data['name'] = $regName;
         }
		if (!isset($data['name']))
			$data['name'] = $this->db->creID($this->table, 'name', 'U', '00');
		
        // info
        $d = $p['d'];
        $data['compellation'] = $d['compellation'];
        $d['sex'] && $data['sex'] = $d['sex'];
        $b = $this->db->save($this->table, $data);
        if (!$b)
            return 'save fail';
        $id = $this->db->insert_id();
        \log::write($this->table, 'reg', $id);
    }
	function isHas($v, $c='name')
	{
		return $this->db->getOne($this->table, '*',  $c. '="'. $this->db->escape($v)  .'"');
	}
}
